package hikari.pathfind;

import hikari.pathfind.data.editor.IndexPair;
import hikari.pathfind.data.editor.Triangle;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Dmitry
 */
public class PolygonProcessor {
    public List<IndexPair> getPolygonOutline(List<Triangle> triangles) {
        Set<IndexPair> outlinePairs = new HashSet<>();
        for (Triangle triangle : triangles) {
            addIndex(new IndexPair(triangle.getIndex1(), triangle.getIndex2()), outlinePairs);
            addIndex(new IndexPair(triangle.getIndex1(), triangle.getIndex3()), outlinePairs);
            addIndex(new IndexPair(triangle.getIndex2(), triangle.getIndex3()), outlinePairs);
        }

        return new ArrayList<>(outlinePairs);
    }

    private void addIndex(IndexPair pair, Set<IndexPair> outlinePairs) {
        if (outlinePairs.contains(pair)) {
            outlinePairs.remove(pair);
            return;
        }

        outlinePairs.add(pair);
    }
}
